package com.klhappy.houyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.Gson;
import com.klhappy.houyuan.adapter.ExMoAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.ExtractMoneyBean.LoginBean;
import com.klhappy.houyuan.bean.MoneyBean.Allsale;
import com.klhappy.houyuan.bean.MoneyBean.ExMoBean;
import com.klhappy.houyuan.bean.MoneyBean.Records;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.JiaYuanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.view.WholeListView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by MrPu on 15/11/21.
 */
public class ExtractMoneyActivity extends AbsActivity implements CallBackInterModle{
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.tv_all_yeji)
    TextView yeji;
    @InjectView(R.id.tv_all_ticheng)
    TextView ticheng;
    @InjectView(R.id.tv_tixian)
    TextView tixian;
    @InjectView(R.id.tv_get)
    TextView tv_get;
    @InjectView(R.id.lv_record)
    WholeListView listView;

    @OnClick(R.id.click_ra)
    public void click_ra(){
        finish();
    }

    @OnClick(R.id.tv_get)
    public void tv_get(){
        if (null == allsales.get(0).getAvlmoney()){
            showToastx("亲,您没有提现余额.");
        }else {
            if (flag){
                Intent intent = new Intent(this ,ExtractNextMoneyActivity.class);
                intent.putExtra("money" ,allsales.get(0).getAvlmoney());
                startActivity(intent);
            }else {
                showToastx("上次提现正在审核中,请等待...");
            }
        }
    }

    private JiaYuanModle modle;
    private ExMoBean moBean;
    private LoginBean loginBean;
    private List<Records> records;
    private List<Allsale> allsales;
    private ExMoAdapter adapter;
    private boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.extract_money);
        ButterKnife.inject(this);
        title.setText("业绩详情");

        modle = new JiaYuanModle(this);
        modle.setCallBackInterModle(this);
        listView.setFocusableInTouchMode(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        modle.getYeJi();
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.CONSUME_URL)){
            if (o.getCode() == 200){
                moBean = JsonCode.getClass(o.getEntity() ,ExMoBean.class);
                records = JsonCode.getClassList(moBean.getRecords(), Records.class);
                allsales = JsonCode.getClassList(moBean.getAllsale() ,Allsale.class);
                try {
                    setView();
                }catch (Exception e){

                }
            }
        }else if (url.equals(UrlConstant.LOGIN_URL)){
            if (o.getCode() == 200){
                mConfigManage.remove(KeyConstant.accname);
                mConfigManage.remove(KeyConstant.Account);
                loginBean = JsonCode.getClass(o.getEntity() ,LoginBean.class);
                mConfigManage.set(KeyConstant.accname ,loginBean.getAccname());
                mConfigManage.set(KeyConstant.Account ,loginBean.getAccount());
            }else {
                showToastx(o.getMsg());
            }
        }
    }

    private void setView(){
        if (null != allsales.get(0).getOr_amount() && !"".equals(allsales.get(0).getOr_amount())){
            yeji.setText(allsales.get(0).getOr_amount() + "元");
        }
        if (null != allsales.get(0).getMoneys() && !"".equals(allsales.get(0).getMoneys())){
            ticheng.setText(allsales.get(0).getMoneys() + "元");
        }
        if (null != allsales.get(0).getAvlmoney() && !"".equals(allsales.get(0).getAvlmoney())){
            tixian.setText(allsales.get(0).getAvlmoney() + "元");
        }

        adapter = new ExMoAdapter(records ,this);
        listView.setAdapter(adapter);

        if (moBean.getPending() > 0){
            tv_get.setText("提现审核中");
            flag = false;
        }else {
            flag = true;
        }
    }
}
